package de.mhu.mandalaui.table;

import de.mhu.mandalaui.Response;
import de.mhu.mandalaui.attributes.ArrayAttribute;

public class Table extends AbstractTable {

	private TableRow row = null;
	private ArrayAttribute data = null;
	
	@Override
	protected void doInitialize() {
		data = new ArrayAttribute();
		data.setOwner(this);
	}

	@Override
	protected void doDestroy() {
		data = null;
	}

	public ArrayAttribute getDataAttribute() {
		return data;
	}
	
//	public void setDataAttribute(ArrayAttribute data) {
//		this.data = data;
//	}
	
	@Override
	public void render(Response response) {
		response.startTag("table", "table", this);
		if (getHeader() != null) {
			response.startTag("thead", null, null);
			response.containerEnter(getHeader());
			getHeader().render(response);
			response.containerLeave();
			response.endTag("thead");
		}

		data.startRender(response);
		response.startTag("tbody", null, null, "data-bind","foreach: " + data.getBindCall() );
		
		response.containerEnter(getRow());
		getRow().render(response);
		response.containerLeave();

		response.endTag("tbody");
		
		response.endTag("table");
	}

	public TableRow getRow() {
		return row;
	}

	public void setRow(TableRow row) {
		if (this.row != null) this.row.componentDetach();
		this.row = row;
		this.row.componentAttach(this);
	}

}
